<%@page contentType="text/html;charset=UTF-8" language="java"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Date"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="javax.jdo.Query"%>
<%@page import="de.upb.messerschmidt.set.entity.Event"%>
<%@page import="de.upb.messerschmidt.set.entity.Room"%>
<%@page import="de.upb.messerschmidt.set.entity.Person"%>
<%@page import="de.upb.messerschmidt.set.entity.Location"%>
<%@page import="de.upb.messerschmidt.set.jdo.PMF"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Talk Registration</title>
</head>

<body>
	<h1>Talk Registration</h1>
	<p>Please choose a room and submit</p>
	<form action="post/talk" method="post">
		<!-- Locations mit JSP abrufen -->
		<%
			Location location = null;
			String event = request.getParameter("event");
			String speaker = request.getParameter("speaker");
			String topic = request.getParameter("topic");
			String description = request.getParameter("description");
			String date = request.getParameter("date");
			String timeFrom = request.getParameter("timeFrom");
			String timeTo = request.getParameter("timeTo");
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query eventQuery = pm.newQuery(Event.class);
			eventQuery.setFilter("title == titleParam");
			eventQuery.declareParameters("String titleParam");
			@SuppressWarnings("unchecked")
			// We need to split the event parameter
			List<Event> events = (List<Event>) eventQuery.execute(event
					.split(":")[0]);
			if (!events.isEmpty()) {
				Event eventObject = events.get(0);
				Query locationQuery = pm.newQuery(Location.class);
				locationQuery.setFilter("name == locationName");
				locationQuery.declareParameters("String locationName");
				@SuppressWarnings("unchecked")
				List<Location> results = (List<Location>) locationQuery
						.execute(eventObject.getLocation());
				if (!results.isEmpty()) {
					location = results.get(0);
				}
		%>
		<table>
			<tr>
				<td>Room:</td>
				<td><select name="room" size="1">
						<%
							Query roomQuery = pm.newQuery(Room.class);
								roomQuery.setOrdering("roomNr desc");
								try {
									@SuppressWarnings("unchecked")
									List<Room> rooms = (List<Room>) roomQuery.execute();
									if (!rooms.isEmpty()) {
										for (Room r : rooms) {
											for (Room i : location.getRooms()) {
												if (r.getKey().equals(i.getKey())) {
						%>
						<option>
							<%=r.getRoomNr() + " at "
											+ r.getLocation().getName()%>
						</option>
						<%
							}
											}

										}
									}
								}

								catch (Exception e) {
									e.printStackTrace();
								}
						%>
				</select>
				</td>
			</tr>
			<tr>
				<td>
					<!-- Pass the parameters from the last form.. --> <input
					type="hidden" name="event" value="<%=event%>"> <input
					type="hidden" name="date" value="<%=date%>"> <input
					type="hidden" name="speaker" value="<%=speaker%>"> <input
					type="hidden" name="topic" value="<%=topic%>"> <input
					type="hidden" name="description" value="<%=description%>">
					<input type="hidden" name="timeFrom" value="<%=timeFrom%>">
					<input type="hidden" name="timeTo" value="<%=timeTo%>"></td>
				<td><input type="submit" value="Submit" />
				</td>
				<td><input type="reset" value="Reset">
				</td>
			</tr>
		</table>
		<%
			} else {
				response.getWriter().println("Please select a valid event");
			}
		%>
	</form>
</body>
</html>
